﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FieldLib.UI;
using Semantics;
namespace FieldDevice.UI
{
    public partial class MeterListViewControl : CollapsibleGroupedListView
    {
        public MeterListViewControl()
        {
            InitializeComponent();
            this.toolStripMenuItemConfigDevice.Click += new EventHandler(toolStripMenuItemConfigDevice_Click);
            this.toolStripMenuItemMonitorComm.Click += new EventHandler(toolStripMenuItemMonitorComm_Click);
            this.toolStripMenuItemOpen.Click += new EventHandler(toolStripMenuItemOpen_Click);
            this.CheckBoxes = FieldLib.SystemConfig.GlobalSystemConfig.EnableEnableDisableDevices;

        }

        void toolStripMenuItemOpen_Click(object sender, EventArgs e)
        {
            if(this.rightclickeddevice==null)
                return;
            this.Open(this.rightclickeddevice);
        }

        void toolStripMenuItemMonitorComm_Click(object sender, EventArgs e)
        {
            if (this.rightclickeddevice == null)
                return;
            DeviceManager.GlobalDeviceManager.MonitorDevice(this.rightclickeddevice);
        }

        void toolStripMenuItemConfigDevice_Click(object sender, EventArgs e)
        {
            if (this.rightclickeddevice == null)
                return;
            DeviceConfigDialog dlg = new DeviceConfigDialog(this.rightclickeddevice);
            if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                if (this.rightclickeddevice.Config != null)
                {
                    string text2 = this.rightclickeddevice.Config.ToString();
                    if (!this.rightclickeddevice.Enable)
                        text2 += "-停用";
                    this.rightclickeditem.SubItems[2].Text = this.rightclickeddevice.Config.ToString();
                }
            }
        }


        bool InProgram = false;
        public void Load()
        {
            InProgram = true;
            this.Items.Clear();
            foreach (FieldLib.Comm.SerialPort serial in DeviceManager.GlobalDeviceManager.GetSerials())
            {
                ListViewGroup group = this.Groups.Add(serial.SerialPortKey, serial.SerialPortKey);
            }
            base.SetGroupCollapse(GroupState.COLLAPSIBLE);
            SortedList<uint, Device> sort = new SortedList<uint, Device>();
            foreach (Node node in DeviceManager.GlobalDeviceManager.Nodes.Values)
            {
                Device device = (Device)node;
                sort.Add(device.NCPAddress, device);
            }
            foreach (Device device in sort.Values)
            {
                string text = device.NCPAddress.ToString("X") + "/" + device.ModbusAddress + device.DeviceModel +
                    "(" + device.CommPortName + "/" + device.ModbusAddress + ")";
                ListViewItem item = this.Items.Add(text);
                item.Tag = device;
                item.Checked = device.Enable;
                item.SubItems.Add(device.DeviceName + ":" + device.Description);
                item.SubItems.Add(device.DeviceModel);
                string text2 = device.Config.ToString();
                if (!device.Enable)
                    text2 += "-停用";
                item.SubItems.Add(text2);
                item.Group = this.Groups[device.SerialPort.SerialPortKey];
            }
            InProgram = false;
        }

        private void MeterListViewControl_DoubleClick(object sender, EventArgs e)
        {
            if (this.SelectedItems.Count == 0)
                return;
            Device device = (Device)SelectedItems[0].Tag;
            Open(device);
        }

        void Open(Device device)
        {
            try
            {

                DeviceManager.GlobalDeviceManager.UIManager.OpenDevice(device);
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        }

        private void MeterListViewControl_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (InProgram)
                return;
            Device device = (Device)e.Item.Tag;
            device.Enable = e.Item.Checked;
            FieldDevice.DeviceManager.GlobalDeviceManager.Storage.SaveEnableDevice(
               device.DeviceName, device.Enable);
        }

        Device rightclickeddevice = null;
        ListViewItem rightclickeditem = null;
        private void MeterListViewControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                rightclickeditem = this.GetItemAt(e.X, e.Y);
                if (rightclickeditem == null)
                {
                    this.toolStripMenuItemOpen.Enabled = false;
                    this.toolStripMenuItemMonitorComm.Enabled = false;
                    this.toolStripMenuItemConfigDevice.Enabled = false;
                    rightclickeddevice = null;
                }
                else
                {
                    this.toolStripMenuItemOpen.Enabled = true;
                    this.toolStripMenuItemMonitorComm.Enabled = true;
                    this.toolStripMenuItemConfigDevice.Enabled = true;
                    rightclickeditem.Selected = true;
                    rightclickeddevice = (Device)rightclickeditem.Tag;
                }
            }
        }

    }
}
